package com.duowei.diandan.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.duowei.diandan.R;
import com.duowei.diandan.bean.Ticket;
import com.duowei.diandan.constant.PayType;
import com.duowei.diandan.impl.OnTicketClickListener;

import java.util.List;
import java.util.Locale;

/**
 * 消费方式Adapter(云会员消费)
 */

public class TicketAdapter extends RecyclerView.Adapter<TicketAdapter.ViewHolder> {

    private List<Ticket> mList;

    public TicketAdapter(List<Ticket> list) {
        mList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ticket_grid_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Ticket ticket = mList.get(position);
        holder.tv_title.setText(ticket.title);
        holder.tv_desc.setVisibility(View.GONE);

        if (ticket.payType == PayType.STORED_VALUE_TYPE) {
            holder.iv_icon.setImageResource(R.drawable.ic_consume_1);
            holder.tv_money.setText(String.format(Locale.CHINA, "¥%.2f", ticket.money));

        } else if (ticket.payType == PayType.INTEGRAL_TYPE) {
            holder.iv_icon.setImageResource(R.drawable.ic_consume_2);
            holder.tv_money.setText(String.format(Locale.CHINA, "%.2f", ticket.money));

        } else if (ticket.payType == PayType.COUPON_TYPE) {
            holder.iv_icon.setImageResource(R.drawable.ic_consume_3);
            holder.tv_money.setText(String.format(Locale.CHINA, "%.0f张", Float.valueOf(ticket.num)));

            if (ticket.leastCost != null && ticket.leastCost > 0) {
                holder.tv_desc.setVisibility(View.VISIBLE);
                holder.tv_desc.setText(String.format(Locale.CHINA, "消费满%.2f使用", ticket.leastCost));
            }
        }

        holder.root.setOnClickListener(v -> {
            if (mOnTicketClickListener != null) {
                mOnTicketClickListener.onTicketClick(ticket);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        View root;
        ImageView iv_icon;
        TextView tv_title;
        TextView tv_desc;
        TextView tv_money;

        public ViewHolder(View itemView) {
            super(itemView);
            root = itemView;
            iv_icon = itemView.findViewById(R.id.iv_icon);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_desc = itemView.findViewById(R.id.tv_desc);
            tv_money = itemView.findViewById(R.id.tv_money);
        }
    }

    public void setOnTicketClickListener(OnTicketClickListener onTicketClickListener) {
        mOnTicketClickListener = onTicketClickListener;
    }

    private OnTicketClickListener mOnTicketClickListener;
}
